package com.cvmars.jianyou.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.cvmars.jianyou.module.fragment.MessageFragment;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hehaifeng on 2018/11/27.
 */

public class CoutDownTextView extends TextView implements View.OnClickListener {


    public int countTime = 60;

    public CoutDownTextView(Context context) {
        super(context);
    }

    public CoutDownTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setOnClickListener(this);
    }

    public CoutDownTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View view) {

        this.setClickable(false);

        if (onClick != null) {
            onClick.onClick();
        }
    }


    public void onRequest(){

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {

                countTime--;

                if (countTime == 0) {
                    this.cancel();
                    countTime = 60;


                    myHandler.sendEmptyMessage(0);
                } else {
                    myHandler.sendEmptyMessage(1);

                }
            }
        }, 1000, 1000);


    }

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if (msg.what == 0) {

                CoutDownTextView.this.setClickable(true);
                CoutDownTextView.this.setText("获取验证码");
            } else {
                CoutDownTextView.this.setText(countTime + "s后重发");

            }


        }
    };


    OnClick onClick;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public interface OnClick {


        void onClick();
    }
}
